package redis;

import java.time.LocalDate;
import java.time.Year;

public class MyBitMap {
    private byte[] bits;
    private int size;

    //构造器
    public MyBitMap(int size) {
        this.size = size;
        this.bits = new byte[(size + 7) / 8];
    }

    //设置值为1
    public void setBit(int index) {
        if (index >= size) {
            // 自动扩容
            resize(index + 1);
        }
        if (index < 0) {
            throw new IndexOutOfBoundsException("Index out of bounds");
        }
        bits[index / 8] |= (1 << (index % 8));
    }

    //设置值为0
    public void clearBit(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index out of bounds");
        }
        bits[index / 8] &= ~(1 << (index % 8));
    }

    //判断是否为1
    public boolean getBit(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index out of bounds");
        }
        return (bits[index / 8] & (1 << (index % 8))) != 0;
    }

    //获取BitMap大小
    public int getSize() {
        return size;
    }

    // 计算最长连续签到天数
    public int getMaxContinuousSignDays() {
        int maxContinuous = 0;
        int currentContinuous = 0;

        for (int i = 0; i < size; i++) {
            if (getBit(i)) {
                currentContinuous++;
                if (currentContinuous > maxContinuous) {
                    maxContinuous = currentContinuous;
                }
            } else {
                currentContinuous = 0;
            }
        }

        return maxContinuous;
    }

    // 计算最近的最长连续签到天数（从今天向前计算）
    public int getRecentMaxContinuousSignDays() {
        LocalDate today = LocalDate.now();
        int dayOfYear = today.getDayOfYear();
        int currentContinuous = 0;

        // 从今天向前遍历
        for (int i = dayOfYear - 1; i >= 0; i--) {
            if (getBit(i)) {
                currentContinuous++;
            } else {
                break; // 遇到未签到的日期则停止
            }
        }

        // 如果今天也签到了，加上今天
        if (dayOfYear < size && getBit(dayOfYear)) {
            currentContinuous++;
        }

        return currentContinuous;
    }

    // 位图扩容方法
    public void resize(int newSize) {
        if (newSize <= size) {
            return; // 新容量不能小于当前容量
        }

        // 创建新数组
        byte[] newBits = new byte[(newSize + 7) / 8];

        // 复制旧数据
        System.arraycopy(bits, 0, newBits, 0, bits.length);

        // 更新引用和大小
        this.bits = newBits;
        this.size = newSize;
    }

    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        int daysInYear = Year.now().length();
        int dayOfYear = today.getDayOfYear();

        // 初始容量设为当前天数的一半，测试扩容
        MyBitMap mb = new MyBitMap(daysInYear / 2);

        // 模拟连续签到（会触发扩容）
        for (int i = dayOfYear - 5; i <= dayOfYear; i++) {
            mb.setBit(i);
        }

        System.out.println("位图容量：" + mb.getSize());
        System.out.println("最长连续签到天数：" + mb.getMaxContinuousSignDays());
        System.out.println("最近连续签到天数：" + mb.getRecentMaxContinuousSignDays());
    }
}
